package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.util.Log;

import com.example.gameapplication.database.DataUtils;
import com.example.gameapplication.plant.BossPlane;

import java.util.ArrayList;
import java.util.List;

public class BossSkill {
    Resources resources;
    List<BossSkillBullet> skillBullets;

    private boolean isInit = false;
    private boolean isFinish = true;//大招是否结束，默认是true，就是结束状态
    private boolean superSkill;
    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public BossSkill(Resources resources) {
        this.resources = resources;
        init();
    }

    public List<BossSkillBullet> getSkillBullets() {
        return skillBullets;
    }

    public void setSkillBullets(List<BossSkillBullet> skillBullets) {
        this.skillBullets = skillBullets;
    }

    public void init() {
        if (skillBullets == null) {
            skillBullets = new ArrayList<>();
            DataUtils.getExecutorService().execute(initRunnable);
        }
    }

    Runnable initRunnable = new Runnable() {
        @Override
        public void run() {
            addSkillBuller();
        }
    };

    private void addSkillBuller() {
//        这个大招太慢了
//        for (int i = 0; i < 360; i++)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(),Math.PI*i/180));
//        for (int i = 360; i > 0; i--)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(), Math.PI*i/180));
//        for (int i = 0; i < 360; i++)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(), Math.PI*i/180));
        for (int i = 0; i < 20; i++)
            skillBullets.add(new BossSkillBullet(resources, DataUtils.getPaint(), DataUtils.getBossSkillSpeed(), i/ Math.PI));
        isInit = true;
    }


    Runnable reSetRunnable = new Runnable() {
        @Override
        public void run() {
            for (BossSkillBullet skillBullet : skillBullets) {
                skillBullet.setAlive(true);
                skillBullet.setLv(DataUtils.getCurrentBossPlane().getLv());
            }
            isFinish=false;
        }
    };

    //重设大招,并且启动,哪个Boss使用技能传哪个进来，获取技能位置使用
    public synchronized void launchSkill(BossPlane bossPlane,boolean superSkill){
        if (!isFinish)return;
        this.superSkill=superSkill;
        DataUtils.setCurrentBossPlane(bossPlane);
        for (BossSkillBullet skillBullet : skillBullets) {
            skillBullet.setAlive(true);
            skillBullet.setLv(DataUtils.getCurrentBossPlane().getLv());
        }
        isFinish=false;
//        DataUtils.getExecutorService().execute(reSetRunnable);
    }
        int drawCont=0;
    public void drawSelf(Canvas canvas) {
        if (!isInit) return;//大招还没初始化
        if (isFinish) return;//大招结束
        if (superSkill)drawSuperSelf(canvas);
        else drawDefaultSelf(canvas);
       DataUtils.runbossSkillAttack();
    }
    private void drawDefaultSelf(Canvas canvas){
        if (drawCont<skillBullets.size()){
            drawCont++;
            if (drawCont>skillBullets.size()){
                drawCont=skillBullets.size();
            }
        }
        int sun = 0;
        for (int i = 0; i <drawCont; i++) {
            sun += skillBullets.get(i).drawSkillSelf(canvas);
        }
        if (sun == 20) {//全部子弹死亡，不在画大招
            isFinish = true;
            drawCont=0;
        }
    }
    private void drawSuperSelf(Canvas canvas) {
        int sun = 0;
        for (int i = 0; i <skillBullets.size(); i++) {
            sun += skillBullets.get(i).drawSkillSelf(canvas);
        }
        if (sun == 20) {//全部子弹死亡，不在画大招
            isFinish = true;
            drawCont=0;
        }
    }
    public void dataDestroyed(){
        if (skillBullets!=null)
            while (skillBullets.size()>0){
                skillBullets.get(0).dataDestroyed();
                skillBullets.remove(0);
            }
        skillBullets=null;
        isInit=false;
    }
}
